//
//  LastBalanceTableViewCell.m
//  EasyToTesco2.0
//
//  Created by 袖手旁觀 on 16/6/23.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "LastBalanceTableViewCell.h"

#define  Margin 8

@interface LastBalanceTableViewCell()

@property(nonatomic,strong)UIButton *select;
@property(nonatomic,assign)NSInteger rateMoney;
@property(nonatomic,strong)UILabel *realMoney;

@end

@implementation LastBalanceTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self loadLastBalanceUI];
    }
    return self;
}

- (void)loadLastBalanceUI {
    UILabel *moreLabel = [[UILabel alloc] init];
    moreLabel.textColor = DEFAULT_COLOR;
    moreLabel.text = @"注:派现金额不得超过 50,000,一个工作日内到账,节假日顺延,请确保卡号无误";
    moreLabel.numberOfLines = 0;
    moreLabel.font = [UIFont systemFontOfSize:13];
    [self.contentView addSubview:moreLabel];
    
    //    [moreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.centerX.equalTo(self.contentView.mas_centerX);
    //        if (iPhone5 || iPhone6) {
    //            make.top.equalTo(self.contentView.mas_top).with.offset(Margin * 3);
    //        } else {
    //            make.top.equalTo(self.contentView.mas_top).with.offset(Margin * 10);
    //        }
    //        make.width.mas_equalTo(WIDTH * 0.7);
    //        make.height.mas_equalTo(40);
    //    }];
    //
    UIButton * immediatePay = [[UIButton alloc] init];
    [immediatePay setTitle:@"立即申请" forState:UIControlStateNormal];
    immediatePay.backgroundColor = DEFAULT_COLOR;
    immediatePay.titleLabel.font = FONT(15);
    [immediatePay setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    immediatePay.layer.cornerRadius = 5;
    immediatePay.layer.masksToBounds = YES;
    [immediatePay addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:immediatePay];
    //
    //    [immediatePay mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.top.equalTo(moreLabel.mas_bottom).with.offset(Margin * 2);
    //        make.centerX.equalTo(self.contentView.mas_centerX);
    //        make.width.mas_equalTo(WIDTH-Margin*4);
    //        make.height.mas_equalTo(40);
    //    }];
    
    
    
}


- (void)setMoney:(NSInteger)money
{
    _money = money;
    NSLog(@"cell --- :%ld",money);
    _rateMoney = _money;
}

- (void)setRateArray:(NSArray *)rateArray
{
    _rateArray = rateArray;
    
    UILabel *rateLabeL = [[UILabel alloc] init];
    rateLabeL.text = @"到账方式";
    rateLabeL.textColor = DEFAULT_COLOR;
    [self addSubview:rateLabeL];
    rateLabeL.font = FONT(14);
    
    CGFloat leftM = 17;
    
    WeakSelf(self);
    [rateLabeL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakself).offset(leftM);
        make.top.equalTo(weakself).offset(0);
    }];
    
    CGFloat btnW = (WIDTH-leftM*2-(_rateArray.count-1)*10)/_rateArray.count;
    for (int i = 0; i < _rateArray.count; ++i) {
        
        UIButton *button = [[UIButton alloc] init];
        [self addSubview:button];
        
        [button setTitle:[NSString stringWithFormat:@"T +%d",i] forState:UIControlStateNormal];
        [button setTitle:[NSString stringWithFormat:@"T +%d",i] forState:UIControlStateSelected];
        
        button.layer.masksToBounds = true;
        button.layer.cornerRadius = 3;
        
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        button.backgroundColor = [UIColor whiteColor];
        button.layer.borderColor = [UIColor grayColor].CGColor;
        button.layer.borderWidth = 1;
        
        button.tag = i;
        
        if (i == 0) {
            button.selected = true;
            _select = button;
            [button setBackgroundColor:DEFAULT_COLOR];
            button.layer.borderWidth = 0;
        }
        
        
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(rateLabeL.mas_bottom).offset(8);
            make.left.equalTo(rateLabeL).offset(i*(btnW+10));
            make.width.mas_equalTo(btnW);
            make.height.mas_equalTo(35);
        }];
        
        [button addTarget:self action:@selector(rateClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    
    UILabel *real = [[UILabel alloc] init];
    [self addSubview:real];
    real.font = FONT(14);
    real.text = @"实际到账 : ";
    [real mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(rateLabeL);
        make.top.equalTo(rateLabeL.mas_bottom).offset(55);
    }];
    
    _realMoney = [[UILabel alloc] init];
    _realMoney.font= FONT(15);
    //    _realMoney.text = @"0:00";
    _realMoney.textColor = DEFAULT_COLOR;
    [self addSubview:_realMoney];
    [_realMoney mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(real.mas_right).offset(5);
        make.centerY.equalTo(real);
    }];
    
}

- (void)rateClick:(UIButton *)rateBtn
{
    
    CGFloat feiLv = [_rateArray[rateBtn.tag] floatValue];
    //    NSLog(@".3%f",feiLv);
    
    //    _realMoney clea
    _realMoney.text = [NSString stringWithFormat:@"%.3f",_rateMoney * feiLv];
    
    rateBtn.selected = true;
    rateBtn.layer.borderWidth = 0;
    [rateBtn setBackgroundColor:DEFAULT_COLOR];
    
    _select.selected = false;
    _select.layer.borderWidth = 1;
    [_select setBackgroundColor:[UIColor whiteColor]];
    _select = rateBtn;
}



- (void)didClickButton:(UIButton *)immediatePayButton {
    if ([self.delegate respondsToSelector:@selector(didClickImmediatePayButton:)]) {
        [self.delegate didClickImmediatePayButton:immediatePayButton];
    }
}

@end
